package net.chrisrichardson.ec2deployer.cluster

import groovy.mock.interceptor.MockFor

class ClusterDefinitionManagerTest extends GroovyTestCase {

    ClusterSpec spec
    ClusterDefinitionManager cdm
    ClusterDefinition definition

    protected void setUp() {
        cdm = new ClusterDefinitionManager()
        spec = new ClusterSpec()
        spec.tomcats(1).slaves(1).webApp("/foo/bar/ptrack", "ptrack").catalinaOptsBuilder("""{builder, databasePrivateDnsName ->
                                     builder.arg("-Xmx500m")
                                     builder.prop("com.sun.management.jmxremote")
                                     builder.prop("com.sun.management.jmxremote.port", 8091)
                                     builder.prop("com.sun.management.jmxremote.authenticate", false)
                                     builder.prop("com.sun.management.jmxremote.ssl", false)
                                     builder.prop("ptrack.application.environment", "ec2")
                                     builder.prop("log4j.configuration", "log4j-minimal.properties")
                                     builder.prop("jdbc.db.server", databasePrivateDnsName)}""").schema("ptrack", ["ptrack": "ptrack"], ["src/test/resources/testdml1.sql", "src/test/resources/testdml2.sql"])
        definition = new ClusterDefinition("foo", spec, ["tomcats": ["a", "b", "c"], "apache": ["z"]])
    }


    public void testBuildXml() {
        StringWriter sw = new StringWriter()
        cdm.buildXml(definition, new PrintWriter(sw))
        // TODO - make an assertion
    }

    public void testParseXml() {
        StringWriter sw = new StringWriter()
        cdm.buildXml(definition, new PrintWriter(sw))
        def xml = sw.toString()

        println xml

        def parser = new XmlParser(false, false)
        def defn = cdm.makeClusterDefinition(parser.parse(new StringReader(xml)))

        def sw2 = new StringWriter()
        cdm.buildXml(defn, new PrintWriter(sw2))
        def xml2 = sw2.toString()

        assertEquals xml, xml2

        assertEquals definition.spec.webApps[0].context, defn.spec.webApps[0].context 
        assertEquals definition.spec.schemaSpec.scripts, defn.spec.schemaSpec.scripts
        assertEquals definition.spec.numberOfTomcats, defn.spec.numberOfTomcats  
        assertEquals definition.spec.sslRequired, defn.spec.sslRequired
        assertEquals definition.spec.schemaSpec.users, defn.spec.schemaSpec.users

        assertEquals definition.serverState, defn.serverState
    }

    public void testGetHomeDir() {
        def homeDir = cdm.homeDir
        assertNotNull homeDir
    }

    void testGetCloudToolsDir() {
        def cloudToolsDir = cdm.cloudToolsDir
        assertTrue cloudToolsDir.exists()
    }

    void testRemoveCluster() {
        def mockFile = new MockFor(File)

        mockFile.demand.exists {true}
        mockFile.demand.exists {true}
        mockFile.demand.delete {true}

        mockFile.use {
            cdm.removeCluster("foo")
        }
    }

    void testListClusters() {
        assertNotNull cdm.listClusters()
    }
}